﻿<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
    style="padding: 20px 30px 0 0;">

    <script type='text/html' template>
        <input type='hidden' name='PID' value="{{ d.params.PID || '' }}"/>
    </script>
    <div class='layui-form-item'>
        <label class='layui-form-label'>部门名称<label style="color: red">*</label></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Name' value='{{ d.params.Name || '' }}' lay-verify='required' placeholder='部门名称' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>部门编号<label style="color: red">*</label></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Code' value='{{ d.params.Code || '' }}' lay-verify='required' placeholder='部门编号' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>

    <div class='layui-form-item' id="Groupdiv" style="display:none">
        <label class='layui-form-label'>所属组织</label>
        <div class='layui-input-block'>
            <div class='layui-input-inline' style="width:350px">
                <script type='text/html' template lay-done='layui.data.getSelSource1(d.params)'>
                <select name="groupId" xm-select="groupId" xm-select-radio="" xm-select-search=""
                    xm-select-skin="default">
                    <option value=""></option>
                </select>
                </script>
            </div>
        </div>
    </div>


    <div class='layui-form-item'>
        <label class='layui-form-label'>上级部门</label>
        <div class='layui-input-block'>
            <div class='layui-input-inline' style="width:350px">
                <script type='text/html' template lay-done='layui.data.getSelSource(d.params);'>
                    <select name='ParentID_SelSource' id='ParentID_SelSource' lay-search></select>
                </script>
            </div>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>联系人<label style="color: red">*</label></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='User' value='{{ d.params.User || '' }}' lay-verify='required' placeholder='联系人' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>联系电话<label style="color: red">*</label></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Phone' value='{{ d.params.Phone || '' }}' lay-verify='required' placeholder='联系电话' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class='layui-form-item'>
        <label class='layui-form-label'>地址<label style="color: red">*</label></label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Address' value='{{ d.params.Address || '' }}' lay-verify='required' placeholder='地址' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>


    <div class='layui-form-item'>
        <label class='layui-form-label'>状态</label>

        <div class='layui-input-block'>
            <script type='text/html' template>
                    <select name='State_Sel'>
                        <option value='1' {{ d.params.State_Sel == '1' ? 'selected' : '' }}>启用</option>
                      <option value='0' {{ d.params.State_Sel == '0' ? 'selected' : '' }}>停用</option>
                    </select>
                </script>

        </div>
    </div>

    <div class='layui-form-item'>
        <label class='layui-form-label'>备注</label>
        <div class='layui-input-block'>
            <script type='text/html' template>
                <input type='text' name='Content' value='{{ d.params.Content || '' }}' lay-verify='' placeholder='备注' autocomplete='off' class='layui-input'>
            </script>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline">
            <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" value="确认" class="layui-btn">
        </div>
    </div>
</div>

<script type="text/javascript">

    layui.data.getSelSource = function (d) {
        layui.use(['form', 'admin', 'formSelects'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var formSelects = layui.formSelects;

            admin.req({
                url: layui.setter.APIUrl + '/admin/departmentMgr/customerQuery', //实际使用请改成服务端真实接口
                type: 'post',
                done: function (res) {
                    $("#ParentID_SelSource").append("<option value=''>全部</option>");
                    layui.each(res.Data, function (index, eachdata) {
                        var strIsSel = '';
                        if (eachdata.PID == d.ParentID_SelSource)
                            strIsSel = "selected='selected'";
                        $("#ParentID_SelSource").append("<option value='" + eachdata.PID + "' " + strIsSel + ">" + eachdata.Name + "[" + eachdata.Code + "]</option>");
                    });
                    form.render('select');
                }
            });

        });
    }


    layui.data.getSelSource1 = function (d) {
        layui.use(['form', 'admin', 'formSelects'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var formSelects = layui.formSelects;

            formSelects.config('groupId', {
                keyName: 'title',            //自定义返回数据中name的key, 默认 name
                keyVal: 'id',            //自定义返回数据中value的key, 默认 value
                keyChildren: 'children',    //联动多选自定义children
            });


            admin.req({
                url: layui.setter.APIUrl + '/WebAPI/user/userInfo', //实际使用s改成服务端真实接口
                type: 'post',
                done: function (res) {
                    if (res.Data.GroupID == "6E0647FD-105B-41F6-929A-32BC9D1CF5F1") {
                        $("#Groupdiv").attr("style", "display:block;");//显示

                        formSelects.config('groupId', {
                            keyName: 'title',            //自定义返回数据中name的key, 默认 name
                            keyVal: 'id',            //自定义返回数据中value的key, 默认 value
                            keyChildren: 'children',    //联动多选自定义children
                        });

                        admin.req({
                            url: layui.setter.APIUrl + '/admin/groupMgr/getParentGroupdataSelect', //实际使用请改成服务端真实接口
                            type: 'post',
                            done: function (res) {
                                formSelects.data('groupId', 'local', {
                                    arr: res.Data
                                });
                                layui.formSelects.value('groupId', [d.GroupID]);

                                if (d.GroupID) {
                                    departChage(d.GroupID)
                                }
                            }
                        });

                    } else {
                        $("#Groupdiv").attr("style", "display:none;");//显示
                    }
                }
            });


            formSelects.on('groupId', function (id, vals, val, isAdd, isDisabled) {

                if (val.id == "-1") {
                    layer.msg("不能选择全部");
                    return false;
                }

                if (!val.id) {
                    $('#ParentID_SelSource').empty();
                    form.render('select');
                } else {

                    departChage(val.id)
                }
            });


            function departChage(groupId) {
                admin.req({
                    url: layui.setter.APIUrl + '/admin/departmentMgr/customerQuery', //实际使用请改成服务端真实接口
                    type: 'post',
                    data: { "groupId": groupId },
                    done: function (res) {
                        debugger
                        $('#ParentID_SelSource').empty();
                        $("#ParentID_SelSource").append("<option value=''>全部</option>");
                        layui.each(res.Data, function (index, eachdata) {
                            var strIsSel = '';
                            if (eachdata.PID == d.ParentID_SelSource)
                                strIsSel = "selected='selected'";
                            $("#ParentID_SelSource").append("<option value='" + eachdata.PID + "' " + strIsSel + ">" + eachdata.Name + "[" + eachdata.Code + "]</option>");
                        });
                        form.render('select');
                    }
                });
            }



        })
    };



    layui.use(['form'], function () {
        var form = layui.form;//required（必填项）phone（手机号）email（邮箱）url（网址）number（数字）date（日期）identity（身份证） 自定义值
    });
</script>